package com.scau.controller;

import com.scau.entity.SingleUserVO;
import com.scau.entity.User;
import com.scau.entity.UserVO;
import com.scau.fegin.UserFegin;
import com.scau.util.FileUtil;
import com.scau.util.JwtUtil;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserFegin userFegin;

    @Value("${web.upload-path}")
    private String fileprefix;

//    @PostMapping(value = "/login",produces = "application/json;charset=UTF-8;")
//    @ResponseBody
//    public User login(@RequestBody User user){
//        User user1 = userFegin.login(user.getId(),user.getPassword(),user.getType());
//        System.out.println(user1);
//        return user1;
//
//    }

    @PostMapping(value = "/login",produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public SingleUserVO login(User user){
        User user1 = userFegin.login(user.getId(),user.getPassword(),user.getType());
        System.out.println(user1);
        JwtUtil jwtUtil = new JwtUtil();
        Map<String, Object> chaim = new HashMap<>();
        chaim.put("username", Integer.toString(user.getId()));
        String token = jwtUtil.encode(Integer.toString(user.getId()), 5 * 60 * 1000, chaim);
        return  new SingleUserVO(0,"",user1,token);
    }

    @GetMapping(value = "/redirect/{location}",produces = "application/json;charset=UTF-8;")
    public String redirect(@PathVariable("location") String location){
//        System.out.println(location);
        log.info("请求跳转  " + location);
        return location;
    }

    @PostMapping(value = "/save",produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public String save(User user,@RequestParam(value = "image" ,required = false) MultipartFile image){

        String filePath = "/uploads/"+user.getId()+"/";

        String fileName = image.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String pid = UUID.randomUUID().toString().replace("-","");
        fileName =  pid+suffix;

        System.out.println(filePath+fileName);

        System.out.println(this.fileprefix+user.getId()+"\\"+fileName);

        FileUtil.createDir(this.fileprefix+user.getId());
        FileUtil.saveFile(image,this.fileprefix+user.getId()+"\\"+fileName);

        user.setLogoUrl(filePath+fileName);
        userFegin.save(user);
        return "success";
    }

    @GetMapping("/findAll")
    @ResponseBody
    public UserVO findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        System.out.println(page + " " +limit);
        return userFegin.findAll((page-1)*limit,limit);
    }

    @GetMapping("/findById/{id}")
    @ResponseBody
    public User findById(@PathVariable("id") int id){
        System.out.println(id);
        User user = userFegin.findById(id);
        log.info("User   findById");
        return user;
    }

    @PostMapping(value = "/update")
    @ResponseBody
    public String update(User user,@RequestParam(value = "image" ,required = false) MultipartFile image){
        System.out.println(user);

        String filePath = "/uploads/"+user.getId()+"/";

        String fileName = image.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String pid = UUID.randomUUID().toString().replace("-","");
        fileName =  pid+suffix;

        System.out.println(filePath+fileName);

        System.out.println(this.fileprefix+user.getId()+"\\"+fileName);

        FileUtil.deleteDir(this.fileprefix+user.getId());
        FileUtil.createDir(this.fileprefix+user.getId());
        FileUtil.saveFile(image,this.fileprefix+user.getId()+"\\"+fileName);

        user.setLogoUrl(filePath+fileName);
        userFegin.update(user);
        return "success";
    }


}
